import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wolfram.jlink.KernelLink;
import com.wolfram.jlink.MathLinkException;
import com.wolfram.jlink.MathLinkFactory;

public class BistableTest {

	public static String codePart1(String tests, ArrayList<String> eqn,
			int numberOfActivators) {
		int funVarsCnt = 0;
		if (eqn.get(3).length() < 4) {
			return "0";
		}
		String strCmd = "eqs[" + eqn.get(0) + "] := First[{ " + eqn.get(4);
		for (int i = 5; i < 4 + numberOfActivators; ++i) {
			strCmd = strCmd + ", " + eqn.get(i);
		}
		strCmd = strCmd + "}/. Solve[{" + eqn.get(4 + numberOfActivators);
		for (int i = 5 + numberOfActivators; i < eqn.size(); ++i) {
			strCmd = strCmd + "," + eqn.get(i);
		}
		strCmd = strCmd + "}==Table[0,{Length[" + eqn.get(3) + "]}],"
				+ eqn.get(3) + "]];";
		strCmd += eqn.get(2);
		strCmd += "Linhib = 100;";
		strCmd += "nvar = Length[var];";
		strCmd += "allPositiveReals[l_] := And @@ (Element[#[[2]], Reals] && #[[2]] >= 0 & /@ l);";

		strCmd += "sols["
				+ eqn.get(0)
				+ "] := DeleteDuplicates[Select[Chop[NSolve[eqs["
				+ eqn.get(1)
				+ "] == Table[0, {nvar}], var, WorkingPrecision -> 10]], allPositiveReals]];";
		strCmd += "Foncs := If[Length[#] == 1, Evaluate[#[[1]]] < 0,Evaluate[#[[1]]] < 0 && Foncs[Delete[#, 1]]] &;";
		strCmd += "jac[" + eqn.get(0) + "] = D[eqs[" + eqn.get(1)
				+ "], {var, 1}];";
		strCmd += "stab[x_, y_, n_] := Re[Eigenvalues[Evaluate[y /. x[[n]]]]];";
		strCmd += "stabsols[listofsols_, jac_] := DeleteCases[Table[If[Foncs[stab[listofsols, jac, i]], listofsols[[i]]], {i, 1, Length[listofsols]}], Null];";
		strCmd += "solutions = stabsols[sols[" + tests + "], jac[" + tests
				+ "]];";
		strCmd += "ToString[solutions]";
		return strCmd;
	}

	public static String codePart2(String tests) {
		return "Length[solutions]";
	}

	public static String codePart3() {
		return "Length[Flatten[solutions]]";
		/*
		 * return "Length[Select[stabilityresults,#>=" +
		 * CodeBuilder.intermediarygoal + "&& #<" + CodeBuilder.nstability +
		 * "&]]";
		 */}

	public static void main(String[] args) {
		String[] tmp = args[0].split("---");
		String tests = tmp[0];
		ArrayList<String> eqn = new ArrayList<String>();
		for (int i = 1; i < tmp.length - 1; i++) {
			eqn.add(tmp[i]);
		}
		int numberOfActivators = Integer.valueOf(tmp[tmp.length - 1]);

		InputStream in = BistableTest.class.getResourceAsStream("execpath");
		StringBuffer fileData = new StringBuffer();
		try {
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			String line = null;
			while ((line = reader.readLine()) != null) {
				fileData.append(line);
			}
			reader.close();
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String test = "";
		int result = 0;
		String strCmd1 = codePart1(tests, eqn, numberOfActivators);
		String strCmd2 = codePart2(tests);
		// For testing
		/*
		 * strCmd =
		 * "LaunchKernels[];eqs[taa_, tbb_, tbc_, tcc_, tbd_, tcd_, tdd_] := " +
		 * "First[{taa*a/(1 + a) - 0.6378*a," +
		 * "tbb*b/(1 + b + Linhib*Ibb) - 0.6378*b," +
		 * "tbc*b/(1 + b + Linhib*Ibc) + tcc*c/(1 + c + Linhib*Icc) -" +
		 * "0.6378*c," +
		 * "tbd*b/(1 + b + Linhib*Ibd) + tcd*c/(1 + c + Linhib*Icd) +" +
		 * "tdd*d/(1 + d) - 0.6378*d} /." +
		 * "Solve[{7.0*c/(1 + c) - 0.6378*Ibb, 90.0*d/(1 + d) - 0.6378*Ibc," +
		 * "4.0*a/(1 + a) - 0.6378*Ibd, 7.0*b/(1 + b) - 0.6378*Icc," +
		 * "8.0*c/(1 + c) - 0.6378*Icd} ==" +
		 * "Table[0, {Length[{Ibb, Ibc, Ibd, Icc, Icd}]}], {Ibb, Ibc, Ibd," +
		 * "Icc, Icd}]]; var = {a, b, c, d}; Linhib = 100; nvar =" +
		 * "Length[var];" + "allPositiveReals[l_] :=" +
		 * "And @@ (Element[#[[2]], Reals] && #[[2]] >= 0 & /@ l);" +
		 * "sols[taa_, tbb_, tbc_, tcc_, tbd_, tcd_, tdd_] :=" +
		 * "DeleteDuplicates[" + "Select[Chop[" +
		 * "NSolve[eqs[taa, tbb, tbc, tcc, tbd, tcd, tdd] == Table[0, {nvar}],"
		 * + "var, WorkingPrecision -> 10]], allPositiveReals]]; Foncs :=" +
		 * "If[Length[#] == 1, Evaluate[#[[1]]] < 0," +
		 * "Evaluate[#[[1]]] < 0 && Foncs[Delete[#, 1]]] &;" +
		 * "jac[taa_, tbb_, tbc_, tcc_, tbd_, tcd_, tdd_] =" +
		 * "D[eqs[taa, tbb, tbc, tcc, tbd, tcd, tdd], {var, 1}];" +
		 * "stab[x_, y_, n_] := Re[Eigenvalues[Evaluate[y /. x[[n]]]]];" +
		 * "stabsols[listofsols_, jac_] :=" + "DeleteCases[" +
		 * "Table[If[Foncs[stab[listofsols, jac, i]], listofsols[[i]]], {i, 1,"
		 * + "Length[listofsols]}], Null];" + "Bistab[x_, y_] :=" +
		 * "Plus @@ Table[" + "If[Foncs[stab[x, y, i]], 1, 0], {i, 1," +
		 * "Length[x]}]; stabilityresults =" + "Map[{#[[1]], Length[#[[2]]]," +
		 * "Length[#[[3]]], #[[2]]} &, {#[[1]]," +
		 * "stabsols[#[[2]], #[[3]]], #[[2]]} & /@" +
		 * "Parallelize[{#, sols @@ #, jac @@ #} & /@ {{1.0, 7.0*10, 1.0*10," +
		 * "3.0, 3.0, 1.0*100, 1.0*10^-1}, {3.0*10^-1, 1.0*100, 3.0," +
		 * "1.0*100, 5.0, 3.0*10, 7.0*10^-1}, {1.0*100, 3.0, 1.0*10," +
		 * "7.0*10^-1, 5.0, 1.0*10^-1, 1.0*100}, {3.0*10, 7.0*10^-1, 7.0," +
		 * "5.0*10, 5.0*10^-1, 3.0, 7.0*10}, {5.0*10^-1, 7.0, 3.0, 1.0*10," +
		 * "1.0*100, 3.0*10, 1.0*10^-1}, {2.0*100, 7.0*10, 7.0*10," +
		 * "7.0*10^-1, 5.0*10, 5.0*10^-1, 1.0*100}, {3.0*10^-1, 7.0*10," +
		 * "1.0*10, 1.0*10^-1, 1.0*10, 3.0*10^-1, 7.0*10^-1}, {3.0*10^-1," +
		 * "7.0*10^-1, 1.0, 1.0*10, 5.0*10^-1, 5.0*10, 1.0}, {7.0*10," +
		 * "5.0*10^-1, 3.0*10^-1, 5.0*10^-1, 1.0*10^-1, 1.0*10^-1, 1.0}}]];" +
		 * "ToString[stabilityresults // MatrixForm]";
		 */
		// strCmd = "0";
		// strCmd="LaunchKernels[];eqs[taa_, tca_, tbb_, tcb_, tdd_] := First[{ taa * a/ ( 1 +a + Linhib * Iaa) + tca * c/ ( 1 +c + Linhib * Ica) - 0.1653 * a, tbb * b/ ( 1 +b + Linhib * Ibb) + tcb * c/ ( 1 +c + Linhib * Icb) - 0.1653 * b,  - 0.1653 * c, tdd * d/ ( 1 +d + Linhib * Idd) - 0.1653 * d}/. Solve[{100.0 * b/ ( 1 +b) + 1.0 * d/ ( 1 +d) - 0.1653 * Iaa,7.0 * a/ ( 1 +a) + 3.0 * b/ ( 1 +b) + 50.0 * d/ ( 1 +d) - 0.1653 * Ibb,0.01 * a/ ( 1 +a) - 0.1653 * Ica,70.0 * a/ ( 1 +a) - 0.1653 * Icb,7.0 * d/ ( 1 +d) - 0.1653 * Idd}==Table[0,{Length[{ Iaa, Ibb, Ica, Icb, Idd}]}],{ Iaa, Ibb, Ica, Icb, Idd}]];var = {a, b, c, d};Linhib = 100;nvar = Length[var];allPositiveReals[l_] := And @@ (Element[#[[2]], Reals] && #[[2]] >= 0 & /@ l);sols[taa_, tca_, tbb_, tcb_, tdd_] := DeleteDuplicates[Select[Chop[NSolve[eqs[taa, tca, tbb, tcb, tdd] == Table[0, {nvar}], var, WorkingPrecision -> 10]], allPositiveReals]];Foncs := If[Length[#] == 1, Evaluate[#[[1]]] < 0,Evaluate[#[[1]]] < 0 && Foncs[Delete[#, 1]]] &;jac[taa_, tca_, tbb_, tcb_, tdd_] = D[eqs[taa, tca, tbb, tcb, tdd], {var, 1}];stabsols[listofsols_, jac_] := DeleteCases[Table[If[Foncs[stab[listofsols, jac, i]], listofsols[[i]]], {i, 1, Length[listofsols]}], Null];Bistab[x_, y_] := Plus @@ Table[If[Foncs[stab[x, y, i]], 1, 0], {i, 1, Length[x]}];stabilityresults = Map[{#[[1]], Length[#[[2]]], Length[#[[3]]], #[[2]]} &, {#[[1]], stabsols[#[[2]], #[[3]]], #[[2]]} & /@ Parallelize[{#, sols @@ #, jac @@ #} & /@{{7.0, 7.0*10, 1.0, 3.0, 2.0*100},{5.0, 3.0, 2.0*100, 1.0*100, 1.0*10},{1.0*100, 5.0, 5.0*10, 3.0, 5.0*10^-1},{3.0*10, 1.0*10, 3.0*10^-1, 5.0, 1.0},{2.0*100, 5.0*10, 1.0*10^-1, 1.0*10^-1, 7.0},{1.0*10, 3.0*10^-1, 5.0, 7.0, 2.0*100},{5.0*10^-1, 1.0*100, 1.0*100, 1.0*10^-1, 3.0*10^-1},{2.0*100, 1.0*10, 7.0*10, 3.0, 5.0*10^-1},{3.0, 1.0*100, 5.0*10, 1.0, 7.0*10^-1},{1.0*100, 5.0, 7.0*10^-1, 1.0*100, 1.0*100},{7.0*10^-1, 3.0, 1.0*10, 2.0*100, 2.0*100},{1.0*100, 5.0*10, 3.0, 5.0, 3.0*10^-1},{3.0*10^-1, 1.0, 7.0*10^-1, 3.0, 3.0*10},{1.0*10^-1, 7.0*10^-1, 3.0, 3.0*10, 1.0},{1.0*10^-1, 5.0*10^-1, 3.0, 5.0*10^-1, 3.0*10^-1},{1.0*100, 3.0*10^-1, 1.0*10, 5.0*10^-1, 5.0},{1.0*100, 1.0, 7.0*10^-1, 5.0, 3.0*10},{1.0*10^-1, 1.0*10, 7.0*10^-1, 5.0, 7.0},{2.0*100, 7.0*10^-1, 5.0, 5.0*10^-1, 5.0*10^-1},{1.0, 2.0*100, 3.0*10^-1, 3.0, 2.0*100},{3.0, 1.0*10, 3.0*10^-1, 1.0*10^-1, 1.0*10},{7.0*10, 1.0*100, 1.0*10, 5.0*10, 2.0*100},{7.0, 2.0*100, 7.0, 3.0*10^-1, 5.0},{1.0*100, 3.0*10, 5.0*10, 1.0, 5.0*10^-1},{3.0*10^-1, 3.0*10^-1, 5.0*10^-1, 1.0*100, 7.0*10^-1},{3.0*10, 3.0*10^-1, 5.0, 5.0*10, 7.0},{3.0, 7.0, 5.0*10^-1, 7.0, 1.0*100},{7.0*10, 7.0*10, 2.0*100, 2.0*100, 7.0},{3.0*10, 2.0*100, 3.0*10, 7.0, 7.0*10},{7.0*10^-1, 7.0*10, 7.0, 7.0, 7.0},{5.0*10, 3.0, 3.0*10, 3.0, 1.0*100},{2.0*100, 3.0*10^-1, 3.0*10, 7.0*10, 7.0*10^-1},{1.0, 3.0*10, 7.0, 1.0, 2.0*100},{3.0*10, 7.0*10^-1, 2.0*100, 3.0*10, 1.0*100},{1.0*10^-1, 3.0, 5.0, 5.0, 1.0*10^-1},{7.0, 1.0*10^-1, 7.0*10^-1, 7.0, 1.0*10^-1},{5.0*10, 7.0*10, 1.0, 1.0*100, 5.0},{7.0, 3.0, 5.0*10, 3.0*10, 5.0},{7.0*10^-1, 5.0*10^-1, 7.0, 1.0*100, 1.0*10^-1},{5.0*10^-1, 2.0*100, 3.0*10^-1, 7.0, 3.0*10^-1},{1.0*10^-1, 3.0*10, 3.0*10^-1, 3.0*10^-1, 1.0*10^-1},{5.0*10, 7.0, 5.0*10^-1, 1.0*10, 5.0*10^-1},{5.0, 1.0*10^-1, 1.0, 7.0*10^-1, 1.0*100},{5.0*10^-1, 5.0*10, 5.0*10^-1, 3.0*10, 1.0*10^-1},{7.0*10, 3.0*10, 1.0*10, 3.0*10, 7.0*10},{1.0*10, 1.0, 1.0, 7.0*10, 3.0},{3.0*10, 3.0, 7.0*10, 5.0*10^-1, 1.0*10},{2.0*100, 1.0*10, 7.0, 7.0*10, 7.0},{7.0*10, 5.0*10^-1, 1.0*100, 3.0*10^-1, 1.0*10^-1},{1.0*10, 3.0, 1.0, 5.0, 3.0*10^-1},{3.0*10^-1, 5.0, 1.0*10^-1, 3.0, 5.0},{7.0, 1.0, 7.0*10, 7.0*10, 5.0},{5.0*10, 5.0, 1.0*10, 1.0*10^-1, 3.0*10},{3.0*10^-1, 5.0*10^-1, 1.0*10, 1.0*10, 5.0*10^-1},{7.0*10, 2.0*100, 1.0*10^-1, 3.0*10^-1, 3.0*10^-1},{5.0, 3.0, 5.0, 7.0, 3.0*10},{5.0*10^-1, 1.0, 7.0*10^-1, 5.0*10^-1, 3.0*10^-1},{3.0*10, 1.0*10^-1, 1.0*10, 3.0*10, 7.0*10^-1},{3.0, 3.0*10, 5.0*10^-1, 7.0, 7.0},{1.0*100, 1.0*10^-1, 7.0, 5.0*10, 2.0*100},{3.0, 3.0*10, 5.0, 3.0*10^-1, 2.0*100},{1.0*10^-1, 3.0*10^-1, 3.0*10^-1, 5.0*10^-1, 5.0*10^-1},{5.0*10, 1.0*10, 5.0*10^-1, 3.0, 5.0},{1.0*100, 7.0*10^-1, 7.0*10, 7.0, 3.0*10},{1.0*100, 7.0*10^-1, 1.0, 7.0, 5.0},{1.0*100, 1.0*100, 5.0, 5.0*10^-1, 7.0*10},{1.0*100, 1.0, 1.0*100, 7.0*10, 1.0},{1.0*100, 1.0, 1.0*100, 7.0, 5.0*10^-1},{1.0*10^-1, 7.0*10, 5.0*10, 3.0, 5.0*10},{5.0*10, 1.0*10, 5.0*10, 1.0*100, 7.0},{1.0*10, 5.0*10^-1, 7.0*10, 1.0*10, 7.0*10},{3.0*10^-1, 2.0*100, 1.0*100, 1.0*100, 3.0*10},{7.0*10, 7.0*10, 1.0, 2.0*100, 1.0*10^-1},{7.0*10^-1, 5.0*10^-1, 7.0, 3.0, 3.0*10},{3.0, 1.0*10, 7.0*10^-1, 3.0, 1.0*10},{5.0*10^-1, 3.0*10, 1.0*10^-1, 2.0*100, 1.0},{3.0, 1.0*10, 3.0, 5.0, 5.0},{1.0*100, 7.0*10, 1.0*100, 5.0*10, 2.0*100},{1.0*10^-1, 1.0*10^-1, 7.0*10^-1, 7.0*10, 1.0*10^-1},{1.0*10^-1, 1.0, 3.0, 5.0*10, 1.0*100},{1.0, 2.0*100, 7.0, 3.0, 1.0*10},{5.0*10^-1, 2.0*100, 1.0, 7.0, 5.0*10},{7.0*10, 5.0*10^-1, 1.0*100, 5.0*10, 1.0*10^-1},{1.0*10^-1, 3.0, 2.0*100, 5.0*10, 5.0*10^-1},{1.0*10^-1, 1.0*10^-1, 1.0*10, 5.0*10^-1, 7.0},{1.0*10, 1.0, 2.0*100, 1.0*10^-1, 5.0*10},{7.0*10, 5.0*10^-1, 5.0*10, 5.0*10^-1, 1.0*100},{5.0, 1.0, 2.0*100, 1.0*100, 3.0},{7.0*10, 5.0, 1.0*10, 1.0*100, 7.0*10},{1.0*10^-1, 3.0*10, 7.0*10^-1, 7.0*10^-1, 1.0},{1.0*10^-1, 7.0, 7.0*10, 3.0, 7.0*10^-1},{1.0*100, 3.0, 5.0*10, 2.0*100, 7.0*10^-1},{3.0*10, 7.0*10, 5.0*10^-1, 7.0*10, 3.0*10},{3.0*10^-1, 5.0*10^-1, 2.0*100, 5.0*10, 3.0*10},{3.0*10^-1, 5.0*10, 1.0*10, 5.0*10, 7.0},{1.0*10^-1, 5.0, 3.0, 7.0*10^-1, 5.0},{5.0*10, 3.0*10, 7.0*10, 2.0*100, 5.0},{7.0*10^-1, 1.0*100, 7.0*10^-1, 7.0*10, 7.0*10},{3.0*10^-1, 7.0*10^-1, 7.0, 1.0*100, 7.0},{5.0*10, 1.0*100, 7.0*10^-1, 5.0, 1.0*10}}]];ToString[stabilityresults // MatrixForm]";
		// strCmd="LaunchKernels[];eqs[taa_, tab_, tcb_, tdb_, tac_, tbc_, tcc_, tdd_] := First[{ taa * a/ ( 1 +a) - 0.6148 * a, tab * a/ ( 1 +a + Linhib * Iab) + tcb * c/ ( 1 +c) + tdb * d/ ( 1 +d + Linhib * Idb) - 0.6148 * b, tac * a/ ( 1 +a + Linhib * Iac) + tbc * b/ ( 1 +b + Linhib * Ibc) + tcc * c/ ( 1 +c) - 0.6148 * c, tdd * d/ ( 1 +d) - 0.6148 * d}/. Solve[{5.0 * b/ ( 1 +b) + 4.0 * d/ ( 1 +d) - 0.6148 * Iab,20.0 * d/ ( 1 +d) - 0.6148 * Iac,30.0 * a/ ( 1 +a) + 30.0 * c/ ( 1 +c) - 0.6148 * Ibc,70.0 * b/ ( 1 +b) + 20.0 * c/ ( 1 +c) + 70.0 * d/ ( 1 +d) - 0.6148 * Idb}==Table[0,{Length[{ Iab, Iac, Ibc, Idb}]}],{ Iab, Iac, Ibc, Idb}]];var = {a, b, c, d};Linhib = 100;nvar = Length[var];allPositiveReals[l_] := And @@ (Element[#[[2]], Reals] && #[[2]] >= 0 & /@ l);sols[taa_, tab_, tcb_, tdb_, tac_, tbc_, tcc_, tdd_] := DeleteDuplicates[Select[Chop[NSolve[eqs[taa, tab, tcb, tdb, tac, tbc, tcc, tdd] == Table[0, {nvar}], var]], allPositiveReals]];Foncs := If[Length[#] == 1, Evaluate[#[[1]]] < 0,Evaluate[#[[1]]] < 0 && Foncs[Delete[#, 1]]] &;jac[taa_, tab_, tcb_, tdb_, tac_, tbc_, tcc_, tdd_] = D[eqs[taa, tab, tcb, tdb, tac, tbc, tcc, tdd], {var, 1}];stabsols[listofsols_, jac_] := DeleteCases[Table[If[Foncs[stab[listofsols, jac, i]], listofsols[[i]]], {i, 1, Length[listofsols]}], Null];Bistab[x_, y_] := Plus @@ Table[If[Foncs[stab[x, y, i]], 1, 0], {i, 1, Length[x]}];stabilityresults = Map[{#[[1]], Length[#[[2]]], Length[#[[3]]], #[[2]]} &, {#[[1]], stabsols[#[[2]], #[[3]]], #[[2]]} & /@ Parallelize[{#, sols @@ #, jac @@ #} & /@{{5.0, 1.0*100, 3.0*10, 3.0*10^-1, 1.0*100, 7.0, 1.0*10^-1, 3.0*10^-1},{3.0, 3.0*10, 1.0*100, 5.0*10^-1, 3.0, 7.0*10^-1, 3.0*10^-1, 1.0*10},{1.0*100, 1.0*100, 5.0*10, 7.0, 5.0, 1.0*10^-1, 3.0*10^-1, 2.0*100},{7.0*10, 1.0*10, 1.0, 1.0*10^-1, 1.0, 5.0*10^-1, 7.0*10^-1, 2.0*100},{7.0*10, 3.0*10, 1.0, 2.0*100, 7.0, 3.0*10, 1.0*10, 1.0*10},{7.0*10^-1, 1.0*10^-1, 7.0*10^-1, 5.0*10, 3.0*10^-1, 1.0, 5.0*10^-1, 1.0*10},{1.0, 3.0*10^-1, 5.0, 1.0*100, 3.0, 1.0, 3.0*10^-1, 3.0*10},{7.0, 5.0, 7.0*10^-1, 1.0*100, 5.0, 3.0*10^-1, 7.0*10^-1, 5.0*10},{5.0*10, 5.0, 7.0*10, 7.0*10, 3.0*10^-1, 2.0*100, 1.0*10^-1, 7.0*10^-1},{7.0, 5.0*10, 7.0*10^-1, 5.0, 1.0*10, 2.0*100, 5.0*10^-1, 7.0*10^-1},{3.0*10^-1, 1.0, 1.0*100, 7.0*10, 5.0, 5.0*10^-1, 3.0*10, 7.0*10^-1},{1.0*10^-1, 1.0*10, 2.0*100, 5.0*10, 5.0, 3.0*10, 1.0, 1.0*10},{7.0*10, 3.0, 5.0*10^-1, 2.0*100, 7.0, 1.0, 1.0*10^-1, 5.0},{1.0*10^-1, 1.0, 3.0, 1.0*10^-1, 7.0, 3.0, 5.0*10^-1, 5.0},{5.0*10^-1, 3.0, 7.0*10^-1, 5.0, 3.0*10, 5.0, 1.0*10^-1, 7.0},{3.0*10, 3.0*10^-1, 1.0, 7.0*10, 1.0*100, 5.0*10, 1.0*10, 1.0*10^-1},{7.0*10, 7.0, 5.0*10, 3.0, 1.0*10, 3.0*10^-1, 1.0, 1.0*100},{7.0, 3.0*10, 1.0*100, 1.0, 1.0*10^-1, 7.0*10^-1, 1.0*10^-1, 3.0*10},{2.0*100, 1.0*10, 3.0*10^-1, 5.0, 7.0, 3.0, 3.0*10, 1.0*100},{3.0*10, 3.0, 1.0*10^-1, 5.0*10^-1, 1.0*100, 1.0*10, 7.0, 3.0*10^-1},{3.0*10^-1, 7.0*10^-1, 5.0*10, 5.0*10, 1.0, 7.0, 1.0, 2.0*100},{2.0*100, 3.0*10, 1.0*100, 5.0*10, 7.0, 7.0*10^-1, 1.0, 2.0*100},{2.0*100, 5.0*10^-1, 3.0*10, 3.0, 5.0*10^-1, 3.0*10^-1, 5.0*10, 7.0*10^-1},{1.0*100, 5.0*10^-1, 5.0, 7.0, 1.0*100, 5.0, 5.0*10^-1, 1.0*100},{1.0*100, 3.0, 1.0*100, 1.0*10, 7.0*10^-1, 1.0*10, 5.0*10, 3.0*10},{2.0*100, 7.0, 7.0*10, 7.0*10, 3.0, 5.0*10^-1, 7.0*10, 3.0},{5.0*10^-1, 3.0*10, 2.0*100, 3.0*10, 3.0*10^-1, 7.0, 1.0*10, 5.0*10^-1},{3.0*10^-1, 1.0*10^-1, 3.0*10^-1, 1.0, 1.0*10, 7.0*10, 5.0*10^-1, 3.0*10},{7.0, 5.0*10^-1, 1.0*100, 3.0, 7.0, 3.0*10^-1, 7.0*10, 1.0*100},{1.0*100, 7.0, 5.0*10, 3.0*10, 2.0*100, 1.0, 1.0*10^-1, 1.0*10},{3.0, 2.0*100, 7.0, 1.0*10, 1.0, 1.0*10, 7.0, 1.0},{5.0*10, 2.0*100, 7.0*10, 3.0*10, 5.0*10^-1, 3.0*10, 3.0*10^-1, 5.0*10},{7.0, 1.0*100, 3.0, 1.0*100, 5.0*10, 2.0*100, 5.0*10^-1, 5.0},{7.0*10^-1, 5.0*10^-1, 3.0*10, 5.0*10^-1, 3.0*10, 2.0*100, 5.0*10, 5.0*10},{1.0*10, 1.0, 5.0*10^-1, 7.0*10, 7.0*10^-1, 1.0*10, 7.0*10, 1.0*100},{5.0*10, 2.0*100, 1.0*10, 5.0, 7.0, 5.0, 2.0*100, 7.0*10},{7.0*10^-1, 7.0*10, 3.0*10^-1, 3.0, 1.0*10^-1, 3.0*10^-1, 7.0*10, 3.0},{7.0*10, 1.0*10^-1, 1.0*100, 7.0, 5.0, 2.0*100, 3.0*10^-1, 5.0},{5.0, 5.0*10^-1, 2.0*100, 1.0*10^-1, 1.0*10^-1, 5.0, 7.0*10, 1.0*100},{3.0*10, 5.0*10, 7.0, 1.0, 1.0*10^-1, 3.0*10, 1.0*10, 2.0*100},{1.0*10^-1, 7.0*10, 5.0*10, 5.0*10, 3.0*10, 1.0*10^-1, 5.0*10^-1, 3.0*10},{2.0*100, 5.0, 1.0*10^-1, 5.0, 3.0*10, 1.0, 7.0*10, 3.0*10},{3.0*10, 3.0*10, 1.0*100, 7.0, 1.0*10, 3.0*10^-1, 1.0*10, 7.0},{1.0*10^-1, 7.0, 1.0, 7.0*10^-1, 3.0*10^-1, 1.0, 1.0, 5.0},{3.0*10^-1, 7.0*10^-1, 7.0*10^-1, 1.0*100, 7.0*10, 5.0*10^-1, 5.0*10, 5.0*10^-1},{5.0, 7.0*10, 1.0*10^-1, 1.0*100, 1.0*10^-1, 7.0*10, 7.0, 7.0},{7.0*10, 1.0*100, 2.0*100, 7.0*10^-1, 7.0, 5.0*10^-1, 2.0*100, 5.0*10},{5.0, 5.0*10^-1, 5.0, 7.0*10, 3.0, 3.0*10^-1, 3.0, 5.0*10^-1},{1.0*10, 3.0*10, 3.0, 3.0, 5.0*10^-1, 5.0*10, 1.0*10^-1, 1.0*10^-1},{7.0*10^-1, 3.0, 1.0*10^-1, 2.0*100, 5.0*10, 1.0*10^-1, 1.0*10, 7.0*10^-1},{3.0, 3.0*10^-1, 3.0*10, 1.0*10^-1, 5.0, 1.0*10^-1, 5.0*10^-1, 3.0*10^-1},{1.0, 3.0*10, 7.0, 2.0*100, 1.0*10^-1, 1.0*10^-1, 1.0, 5.0*10^-1},{1.0*10, 7.0*10, 1.0*10^-1, 3.0*10^-1, 2.0*100, 3.0, 3.0, 7.0},{7.0*10, 1.0*100, 1.0*100, 7.0, 7.0*10, 5.0, 3.0*10^-1, 5.0},{3.0, 5.0*10, 1.0*10, 5.0, 3.0*10, 7.0*10, 2.0*100, 7.0},{3.0*10, 5.0*10^-1, 3.0, 3.0, 2.0*100, 1.0*100, 7.0*10, 7.0*10},{3.0*10^-1, 3.0, 5.0*10, 5.0, 2.0*100, 1.0*100, 3.0*10, 3.0},{1.0*10^-1, 3.0, 5.0*10, 1.0*10, 1.0*10, 5.0*10^-1, 1.0, 1.0*10},{7.0*10, 2.0*100, 1.0*100, 5.0*10, 5.0*10^-1, 5.0, 5.0*10^-1, 3.0*10^-1},{1.0*100, 1.0*10^-1, 2.0*100, 5.0, 1.0*100, 2.0*100, 3.0*10, 1.0*10},{5.0*10^-1, 7.0*10^-1, 1.0*100, 3.0*10^-1, 1.0, 1.0*100, 1.0, 5.0*10},{3.0, 3.0, 7.0*10^-1, 1.0, 3.0*10^-1, 3.0*10^-1, 7.0*10, 1.0*10},{2.0*100, 1.0*10^-1, 5.0*10^-1, 1.0, 1.0*100, 3.0*10^-1, 7.0*10, 7.0},{1.0*100, 3.0, 1.0*100, 1.0*10^-1, 1.0*100, 5.0*10^-1, 2.0*100, 5.0},{3.0*10, 5.0*10^-1, 3.0*10^-1, 1.0, 3.0*10^-1, 5.0*10, 7.0*10, 5.0*10^-1},{1.0*10^-1, 1.0*10^-1, 5.0, 1.0*10^-1, 7.0*10, 1.0, 5.0*10^-1, 3.0*10},{2.0*100, 1.0*10^-1, 7.0*10, 1.0*100, 2.0*100, 1.0, 1.0*10, 1.0*10},{1.0*100, 3.0*10, 1.0, 7.0, 7.0*10^-1, 1.0*100, 3.0*10^-1, 2.0*100},{3.0*10^-1, 1.0*10, 1.0*10, 3.0*10, 5.0, 3.0, 7.0*10, 7.0*10^-1},{7.0, 2.0*100, 3.0*10^-1, 1.0*10, 1.0*100, 3.0, 1.0*100, 7.0*10^-1},{5.0*10^-1, 7.0*10, 7.0*10, 7.0*10, 1.0*10, 2.0*100, 1.0*10, 3.0*10^-1},{3.0*10, 1.0*100, 7.0*10^-1, 1.0*10, 7.0*10, 5.0*10, 1.0*100, 3.0},{1.0, 1.0*100, 5.0*10, 7.0*10, 3.0, 3.0, 5.0*10^-1, 1.0*10^-1},{7.0*10^-1, 1.0*10, 5.0*10, 3.0*10, 5.0, 2.0*100, 7.0, 3.0},{1.0*10^-1, 7.0, 1.0*10, 3.0*10^-1, 3.0*10^-1, 5.0*10^-1, 7.0, 1.0*10^-1},{1.0, 5.0*10^-1, 1.0, 7.0*10, 5.0*10, 1.0*10, 3.0*10^-1, 7.0},{5.0, 3.0*10, 5.0*10, 1.0*100, 3.0*10^-1, 3.0, 3.0*10^-1, 3.0*10},{5.0*10, 1.0*100, 5.0*10, 2.0*100, 3.0, 7.0*10, 3.0*10, 5.0*10},{7.0, 5.0, 3.0*10, 1.0*100, 3.0*10, 1.0*10^-1, 1.0*10^-1, 1.0*100},{1.0*10^-1, 5.0*10^-1, 3.0*10^-1, 1.0*10^-1, 1.0*10^-1, 7.0*10, 1.0*10, 1.0*100},{1.0*10^-1, 1.0*10^-1, 1.0*100, 1.0*10^-1, 5.0*10^-1, 1.0*100, 1.0*10, 1.0*100},{5.0, 7.0*10, 1.0*100, 1.0*100, 7.0*10, 1.0, 1.0, 5.0*10},{1.0*10, 5.0*10^-1, 1.0*100, 5.0, 1.0, 3.0*10, 5.0, 5.0*10^-1},{3.0*10, 2.0*100, 1.0*10^-1, 3.0, 3.0, 3.0, 3.0*10^-1, 2.0*100},{5.0*10, 1.0*10^-1, 1.0, 1.0*10^-1, 7.0, 5.0*10^-1, 7.0*10, 1.0},{5.0*10, 1.0, 1.0*10^-1, 7.0*10, 5.0*10^-1, 7.0*10, 7.0*10, 5.0},{5.0*10^-1, 7.0*10^-1, 7.0*10^-1, 1.0*10^-1, 5.0*10^-1, 5.0*10, 3.0*10, 3.0*10},{1.0, 2.0*100, 1.0*10, 1.0*100, 3.0, 5.0, 3.0, 3.0*10},{1.0*100, 7.0*10, 5.0*10, 1.0, 1.0*100, 5.0*10, 5.0, 5.0*10},{3.0*10, 1.0, 1.0*10, 1.0*10, 5.0*10, 5.0*10^-1, 5.0*10^-1, 7.0},{5.0, 5.0*10^-1, 7.0*10^-1, 5.0*10^-1, 3.0*10, 5.0*10^-1, 1.0*100, 5.0},{7.0*10, 2.0*100, 1.0*10, 1.0*100, 1.0, 5.0, 2.0*100, 1.0*100},{7.0, 5.0, 1.0*10^-1, 5.0*10^-1, 3.0*10^-1, 5.0*10, 3.0*10^-1, 2.0*100},{5.0, 7.0*10^-1, 1.0*10^-1, 3.0, 7.0*10^-1, 5.0, 1.0*10^-1, 5.0},{1.0, 3.0*10^-1, 2.0*100, 5.0*10, 3.0*10^-1, 7.0*10, 3.0*10, 7.0*10^-1},{3.0*10, 3.0*10, 5.0*10, 1.0*10^-1, 5.0*10^-1, 7.0*10^-1, 7.0, 3.0*10^-1},{5.0*10^-1, 7.0, 1.0, 3.0*10, 1.0*100, 1.0*10, 1.0*10^-1, 3.0},{5.0*10^-1, 7.0*10, 2.0*100, 5.0*10, 5.0, 1.0, 2.0*100, 3.0},{3.0, 5.0*10^-1, 7.0, 3.0*10^-1, 5.0*10^-1, 5.0*10, 5.0*10, 1.0},{1.0, 1.0, 1.0*10, 2.0*100, 5.0*10^-1, 5.0*10, 7.0*10^-1, 7.0*10}}]];ToString[stabilityresults // MatrixForm]";
		int nSolutions = 0;
		String solutions = "";
		int bistableResult = 0;
		if (strCmd1.equals("0")) {
			result = 1;
		} else {
			try {
				final KernelLink ml = MathLinkFactory.createKernelLink(fileData
						.toString());
				final ScheduledExecutorService canceller = Executors
						.newScheduledThreadPool(1);
				canceller.schedule(new Runnable() {
					public void run() {
						ml.abortEvaluation();
						ml.abandonEvaluation();
					}
				}, 10, TimeUnit.SECONDS);
				synchronized (ml) {
					try {
						ml.discardAnswer();
						ml.evaluate(strCmd1);
						ml.waitForAnswer();
						solutions = ml.getString();
						ml.flush();
						ml.evaluate(strCmd2);
						ml.waitForAnswer();
						bistableResult = ml.getInteger();
					} catch (MathLinkException e) {
						if (e.getMessage().equals(
								"Array is not as deep as requested.")) {
							result = 1;
						} else if (e.getMessage().equals(
								"MLGet out of sequence.")
								|| e
										.getMessage()
										.equals(
												"MathLink put or get was aborted before affecting the link.")) {
							result = 2;
						} else {
							result = 3;
						}
						// e.printStackTrace();
					} finally {
						ml.close();
					}
				}
			} catch (Exception e) {
				result = 3;
			}
		}
		// For checking all possible parameters sets from the library
		// exhaustively
		// score = score/(Math.pow(t_length,funVarsCnt)); // normalize the score
		// by dividing with the total number of parameter sets

		// For checking specific number of parameter sets created randomly from
		// the library
		// score = score / gNumberOfRandomParameterSet; // normalize the score
		// by
		// dividing with the
		// total number of
		// parameter sets
		System.out.println(result);
		if (result == 0) {
			System.out.println(bistableResult);
			System.out.println(solutions);
		}
		System.out.println(strCmd1 + ";" + strCmd2);
		System.exit(0);
	}
}
